package vistas;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.HeadlessException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import org.jvnet.substance.SubstanceLegacyDefaultLookAndFeel;
import org.jvnet.substance.api.skin.SubstanceGeminiLookAndFeel;
import org.jvnet.substance.api.skin.SubstanceGraphiteAquaLookAndFeel;
import org.jvnet.substance.api.skin.SubstanceMagellanLookAndFeel;
import org.jvnet.substance.skin.SubstanceAutumnLookAndFeel;
import org.jvnet.substance.skin.SubstanceBusinessBlueSteelLookAndFeel;
import org.jvnet.substance.skin.SubstanceBusinessLookAndFeel;
import org.jvnet.substance.skin.SubstanceChallengerDeepLookAndFeel;
import org.jvnet.substance.skin.SubstanceDustCoffeeLookAndFeel;
import org.jvnet.substance.skin.SubstanceEmeraldDuskLookAndFeel;
import org.jvnet.substance.skin.SubstanceMagmaLookAndFeel;
import org.jvnet.substance.skin.SubstanceMistAquaLookAndFeel;
import org.jvnet.substance.skin.SubstanceMistSilverLookAndFeel;
import org.jvnet.substance.skin.SubstanceModerateLookAndFeel;
import org.jvnet.substance.skin.SubstanceNebulaBrickWallLookAndFeel;
import org.jvnet.substance.skin.SubstanceNebulaLookAndFeel;
import org.jvnet.substance.skin.SubstanceOfficeBlue2007LookAndFeel;
import org.jvnet.substance.skin.SubstanceOfficeSilver2007LookAndFeel;
import org.jvnet.substance.skin.SubstanceRavenGraphiteGlassLookAndFeel;
import org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel;
import org.jvnet.substance.skin.SubstanceRavenLookAndFeel;
import org.jvnet.substance.skin.SubstanceSaharaLookAndFeel;
import org.jvnet.substance.skin.SubstanceTwilightLookAndFeel;

import clases.CV_imprimir;
import clases.Conexion;
import clases.Login_class;
import clases.utilidades_class;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Toolkit;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.SwingConstants;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Loginform extends JFrame {

	private JPanel contentPane;
	private JTextField usuario;
	private JPasswordField contrasena;
	private utilidades_class utilidades;
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					try {
//					    UIManager.setLookAndFeel(new SubstanceModerateLookAndFeel());					    
					    UIManager.setLookAndFeel(new SubstanceNebulaBrickWallLookAndFeel());
//					    UIManager.setLookAndFeel(new SubstanceOfficeSilver2007LookAndFeel());
					} catch (Exception e) {
					    e.printStackTrace();
					}
			                    
					Loginform frame = new Loginform();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Loginform() {
		setUndecorated(true);
		setIconImage(Toolkit.getDefaultToolkit().getImage(Loginform.class.getResource("/icons/logocifco.png")));
		setTitle("CIFCO HRM - Iniciar Sesi\u00F3n");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 303, 285);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setLocationRelativeTo(null);
		utilidades = new utilidades_class();
		

		
		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(Loginform.class.getResource("/icons/logo cifco_mediano.png")));
		label.setBounds(0, 0, 300, 146);
		contentPane.add(label);
		
		JLabel lblUsuario = new JLabel("Usuario");
		lblUsuario.setFont(new Font("Arial", Font.PLAIN, 13));
		lblUsuario.setBounds(30, 164, 46, 14);
		contentPane.add(lblUsuario);
		
		JLabel lblContrasea = new JLabel("Contrase\u00F1a");
		lblContrasea.setFont(new Font("Arial", Font.PLAIN, 13));
		lblContrasea.setBounds(10, 196, 66, 14);
		contentPane.add(lblContrasea);
		
		usuario = new JTextField();
		usuario.setHorizontalAlignment(SwingConstants.CENTER);
		usuario.setFont(new Font("Arial", Font.PLAIN, 13));
		usuario.setBounds(114, 161, 169, 20);
		contentPane.add(usuario);
		usuario.setColumns(10);
		
		contrasena = new JPasswordField();
		contrasena.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				if(e.getKeyCode()==KeyEvent.VK_ENTER)
				{

					try{
						Login_class log =new Login_class(usuario.getText(), utilidades.MD5(contrasena.getText()));
						if(log.ingresar())
						{
							Principal ppal = new Principal(usuario.getText(), log.getNombreusuario());
							ppal.setVisible(true);
							hide();
							
						}
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(null, ex.toString());
					}

				
				}
			}
		});
		contrasena.setHorizontalAlignment(SwingConstants.CENTER);
		contrasena.setFont(new Font("Arial", Font.PLAIN, 13));
		contrasena.setBounds(114, 193, 169, 20);
		contentPane.add(contrasena);
		
		JButton btnIniciarSesion = new JButton("Iniciar Sesion");
		btnIniciarSesion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try{
					Login_class log =new Login_class(usuario.getText(), utilidades.MD5(contrasena.getText()));
					if(log.ingresar())
					{
						Principal ppal = new Principal(usuario.getText(), log.getNombreusuario());
						ppal.setVisible(true);
						hide();
						
					}
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null, ex.toString());
				}

			}
		});
		btnIniciarSesion.setIcon(new ImageIcon(Loginform.class.getResource("/icons/iniciarsesion.png")));
		btnIniciarSesion.setBounds(12, 241, 143, 36);
		contentPane.add(btnIniciarSesion);
		
		JButton btnCancelar = new JButton("Cancelar");
		btnCancelar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnCancelar.setIcon(new ImageIcon(Loginform.class.getResource("/icons/salir.png")));
		btnCancelar.setBounds(158, 241, 135, 36);
		contentPane.add(btnCancelar);
	}
}
